package com.softer.chengxin.score.service.impl;

import com.softer.chengxin.common.domin.*;
import com.softer.chengxin.common.enums.NumberEnum;
import com.softer.chengxin.common.enums.ResultEnum;
import com.softer.chengxin.score.dao.ScoreDetailDao;
import com.softer.chengxin.score.entity.Appeal;
import com.softer.chengxin.score.entity.ScoreDTO;
import com.softer.chengxin.score.entity.ScoreDetail;
import com.softer.chengxin.score.service.ScoreDetailService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Gong Zhiyang
 * @date 2018/9/26
 */
@Service
public class ScoreDetailServiceImpl implements ScoreDetailService {

    @Autowired
    private ScoreDetailDao scoreDetailDao;
    /**
     * 保存信息录入
     *
     * @param scoreDetail 信息实体
     * @param id          企业ID
     * @return 处理结果
     */
    @Override
    public CommonResult addScoreDetail(ScoreDetail scoreDetail, Integer id) {
        int num = scoreDetailDao.addScoreDetail(scoreDetail, id);
        if (num > Integer.parseInt(Common.ZERO)) {
            return new CommonResult(ResultEnum.SUCCESS.getCode(), Common.OPERATE_SUCCESS);
        }
        return new CommonResult(ResultEnum.FAIL.getCode(), Common.OPERATE_FAIL);
    }

    /**
     * 先保存后，提交信息录入，只需要修改状态
     *
     * @param id    得分详情ID
     * @param state 状态
     * @return 处理结果
     */
    @Override
    public CommonResult updateState(Integer id, Integer state) {
        int num = scoreDetailDao.updateState(id, state);
        if (num > Integer.parseInt(Common.ZERO)) {
            return new CommonResult(ResultEnum.SUCCESS.getCode(), "提交成功");
        }
        return new CommonResult(ResultEnum.FAIL.getCode(), "提交失败，稍后重试");
    }

    /**
     * 获取录入信息详情
     *
     * @param id 录入信息Id
     * @return 信息详情
     */
    @Override
    public ScoreDetail getScoreDetail(Integer id) {
        return scoreDetailDao.getScoreDetail(id);
    }

    /**
     * 删除录入信息
     *
     * @param id 录入信息ID
     * @return 处理结果
     */
    @Override
    public CommonResult delete(Integer id) {
        int num = scoreDetailDao.delete(id);
        if (num > Integer.parseInt(Common.ZERO)) {
            return new CommonResult(ResultEnum.SUCCESS.getCode(), "删除成功");
        }
        return new CommonResult(ResultEnum.FAIL.getCode(), "删除失败，稍后重试");
    }

    /**
     * 查询录入信息列表
     *
     * @param page      页面实体
     * @param scoreDTO  查询实体
     * @param loginName 登陆名
     * @return 列表
     */
    @Override
    public ManagerPageResult query(Page page, ScoreDTO scoreDTO, String loginName) {
        ManagerPageResult mr = new ManagerPageResult();
        JsonResult jr = new JsonResult();
        String states = String.valueOf(scoreDTO.getState());
        //State为空则查询所有列表
        if(null == scoreDTO.getState()){
            if (Common.ADD.equals(scoreDTO.getType().trim())) {
                //信息录入页面（查询保存0和公示2的信息）
                states = "0,2";
            } else if (Common.QUERY.equals(scoreDTO.getType().trim())) {
                //信息查询页面（查询审核中1、9，申诉不通过3，申诉中4、10，申诉成功5，申诉失败6，生效7的信息）
                states = "1,3,4,5,6,7,9,10,11,12";
            }
        }
        int num = scoreDetailDao.queryNum(scoreDTO, states, loginName);
        if (num > Integer.parseInt(Common.ZERO)) {
            List<ScoreDetail> list = scoreDetailDao.queryList(page, scoreDTO, states, loginName);
            return setResult(mr, jr, num, list);
        }
        return setResult(mr, jr, Integer.parseInt(Common.ZERO), new ArrayList());
    }

    /**
     * 企业申诉
     *
     * @param appeal 申诉实体
     * @return 结果
     */
    @Override
    public CommonResult appeal(Appeal appeal) {
        int num = scoreDetailDao.appeal(appeal);
        if (num > Integer.parseInt(Common.ZERO)) {
            return new CommonResult(ResultEnum.SUCCESS.getCode(), "提交申诉成功");
        }
        return new CommonResult(ResultEnum.FAIL.getCode(), "提交申诉失败，稍后重试");
    }

    /**
     * 获取最近的信息（用于首页展示）
     *
     * @param id        企业ID
     * @param projectId 项目ID
     * @return 信息集合
     */
    @Override
    public List<ScoreDetail> getCurrentMsg(Integer id, String projectId) {
        return scoreDetailDao.getCurrentMsg(id, projectId);
    }

    /**
     * 编辑录入信息
     *
     * @param scoreDetail 录入信息实体
     * @return 录入结果
     */
    @Override
    public CommonResult edit(ScoreDetail scoreDetail) {
        int num = scoreDetailDao.edit(scoreDetail);
        if (num > Integer.parseInt(Common.ZERO)) {
            return new CommonResult(ResultEnum.SUCCESS.getCode(), Common.OPERATE_SUCCESS);
        }
        return new CommonResult(ResultEnum.FAIL.getCode(), Common.OPERATE_FAIL);
    }

    /**
     * 查询综合查询的年度集合
     *
     * @return 年度集合
     */
    @Override
    public List<String> getYearList() {
        return scoreDetailDao.getYearList();
    }

    /**
     * 设置返回参数
     *
     * @param mpr  ManagerPageResult
     * @param jr   JsonResult
     * @param num  数量
     * @param list 集合列表
     */
    private ManagerPageResult setResult(ManagerPageResult mpr, JsonResult jr, int num, List list) {
        mpr.setCount(num);
        jr.setDs(list);
        mpr.setJson(jr);
        return mpr;
    }
}
